package com.cx.update;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel.MapMode;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    protected static char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    protected static MessageDigest messagedigest = null;

    static {
        try {
            messagedigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var1) {
            var1.printStackTrace();
        }

    }

    public MD5Util() {
    }

    private static void appendHexPair(byte paramByte, StringBuffer stringBuffer) {
        char c1 = hexDigits[(paramByte & 240) >> 4];
        char c2 = hexDigits[paramByte & 15];
        stringBuffer.append(c1);
        stringBuffer.append(c2);
    }

    private static String bufferToHex(byte[] paramArrayOfByte) {
        return bufferToHex(paramArrayOfByte, 0, paramArrayOfByte.length);
    }

    private static String bufferToHex(byte[] paramArrayOfByte, int paramInt1, int paramInt2) {
        StringBuffer localStringBuffer = new StringBuffer(paramInt2 * 2);
        int i = paramInt1 + paramInt2;

        for(int j = paramInt1; j < i; ++j) {
            appendHexPair(paramArrayOfByte[j], localStringBuffer);
        }

        return localStringBuffer.toString();
    }

    public static boolean checkPassword(String paramString1, String paramString2) {
        return getMD5String(paramString1).equals(paramString2);
    }

    public static String getFileMD5String(File file) {
        try {
            FileInputStream e = new FileInputStream(file);
            byte[] arrayOfByte = new byte[1024];

            while(true) {
                int i = e.read(arrayOfByte);
                if(i <= 0) {
                    e.close();
                    return bufferToHex(messagedigest.digest());
                }

                messagedigest.update(arrayOfByte, 0, i);
            }
        } catch (Exception var4) {
            var4.printStackTrace();
            return "";
        }
    }

    public static String getFileMD5String_old(File paramFile) throws IOException {
        MappedByteBuffer localMappedByteBuffer = (new FileInputStream(paramFile)).getChannel().map(MapMode.READ_ONLY, 0L, paramFile.length());
        messagedigest.update(localMappedByteBuffer);
        return bufferToHex(messagedigest.digest());
    }

    public static String getMD5String(String paramString) {
        return getMD5String(paramString.getBytes());
    }

    public static String getMD5String(byte[] paramArrayOfByte) {
        messagedigest.update(paramArrayOfByte);
        return bufferToHex(messagedigest.digest());
    }
}
